#include "EconomyPlugin.h"
#include "Expand/PluginsManager.h"
#include "Managers/GameManager.h"
#include "GameSingleton.h"
#include "Log.h"

#include "EconomyAPI.h"
#include "EconomyHome/EconomyHome.h"
#include "EconomyLand/EconomyLand.h"
#include "EconomyShop/EconomyShop.h"


namespace Economy
{
    const std::string EconomyPlugin::PluginDataDirectory = "./Plugins/Economy";

    EconomyPlugin::EconomyPlugin()
    {
        m_economyAPI = GameSingleton::singletonPtr->registerSingleton<EconomyAPI>();
        m_economyHome = GameSingleton::singletonPtr->registerSingleton<EconomyHome>();
        m_economyLand = GameSingleton::singletonPtr->registerSingleton<EconomyLand>();
        m_economyShop = GameSingleton::singletonPtr->registerSingleton<EconomyShop>();
    }

    void EconomyPlugin::load()
    {
        m_economyAPI->load();
        m_economyHome->load();
        m_economyLand->load();
        m_economyShop->load();
    }

    void EconomyPlugin::save()
    {
        if(std::filesystem::exists(PluginDataDirectory) == false)
        {
            if(std::filesystem::create_directories(PluginDataDirectory) == false)
            {
                throw std::runtime_error("检测不到 经济插件 数据文件夹，并且无法创建新的 经济插件 数据文件夹");
            }
        }
        m_economyAPI->save();
        m_economyHome->save();
        m_economyLand->save();
        m_economyShop->save();
    }

    void EconomyPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/money");
        cmdVec.push_back("/land");
        cmdVec.push_back("/shop");
        cmdVec.push_back("/ehome");
    }

    void EconomyPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(cmdDataVec[0] == "/money")
        {
            m_economyAPI->processCmd(chatObj, cmdDataVec);
        }
        else if(cmdDataVec[0] == "/ehome")
        {
            m_economyHome->processCmd(chatObj, cmdDataVec);
        }
        else if (cmdDataVec[0] == "/land")
        {
            m_economyLand->processCmd(chatObj, cmdDataVec);
        }
        else if (cmdDataVec[0] == "/shop")
        {
            m_economyShop->processCmd(chatObj, cmdDataVec);
        }
    }

    const std::string &EconomyPlugin::getPluginName()
    {
        static const std::string pluginName = "经济插件";
        return pluginName;
    }

    const std::string &EconomyPlugin::getIntroduce()
    {
        static const std::string introduce = "一整套金钱系统";
        return introduce;
    }

    uint32_t EconomyPlugin::getUseAuthLevel()
    {
        return 0;
    }

    AuthDisplayType EconomyPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

} // namespace PocketSurvival

extern "C" void initialize()
{
    GameSingleton::pluginsManager->registerPlugin<Economy::EconomyPlugin>();
    Log::Info("成功加载 经济 插件");
}
